package com.ldn.basic.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author 李大牛家的牛 (scafel@163.com)
 * @createDate 2024/7/9 - 17:21
 * @description ...
 **/
@Slf4j
@Component
public class AppAnnotationUtil {

    Class<? extends Annotation> annotationClass;
    Class<? extends Annotation> annotationMethod;
    /** 参数可以为空 **/
    Object args;

    // 运行指定注解类的指定注解方法
    private void runAnnotationClassMethod() {
        Map<String, Object> beansWithAnnotationMap = AppSpringContextUtil.getBeansWithAnnotation(annotationClass);
        for (Map.Entry<String, Object> entry : beansWithAnnotationMap.entrySet()) {
            Method[] methods = entry.getValue().getClass().getMethods();
            for (Method method: methods) {
                if (org.springframework.core.annotation.AnnotationUtils.findAnnotation(method , annotationMethod) != null){
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append("调用");
                    stringBuilder.append(entry.getValue().getClass().getName()).append("类下的");
                    stringBuilder.append(method.getName()).append("方法");
                    try {
                        Object cla = AppSpringContextUtil.getBean(entry.getValue().getClass());
                        method.invoke(cla, this.args);
                    } catch (Exception e) {
                        stringBuilder.append("失败----");
                        stringBuilder.append(e.getMessage());
                        log.error(stringBuilder.toString());
                    }
                }
            }
        }
    }

    @Async
    public void runMethod(
            Class<? extends Annotation> annotationClass ,
            Class<? extends Annotation> annotationMethod ,
            Object request,
            Object response,
            Object params
    ) {
        this.annotationClass = annotationClass;
        this.annotationMethod = annotationMethod;
        log.warn("开始执行注解方法 => {}", annotationClass.getName()+ "." + annotationMethod.getName());
        runAnnotationClassMethod();
    }

    @Async
    public void runMethod(
            Class<? extends Annotation> annotationClass ,
            Class<? extends Annotation> annotationMethod ,
            Object request,
            Object response
    ) {
        runMethod(annotationClass, annotationMethod, request, response, "");
    }

    @Async
    public void runMethod(
            Class<? extends Annotation> annotationClass ,
            Class<? extends Annotation> annotationMethod ,
            Object request
    ) {
        runMethod(annotationClass, annotationMethod, request, "", "");
    }

    @Async
    public void runMethod(
            Class<? extends Annotation> annotationClass ,
            Class<? extends Annotation> annotationMethod
    ) {
        runMethod(annotationClass, annotationMethod, "", "", "");
    }

    public Map<String , Object> getAnnotationClass (
            Class<? extends Annotation> annotationClass
    ) {
        return AppSpringContextUtil.getBeansWithAnnotation(annotationClass);
    }

}
